package org.jeecg.modules.xxgx.kpzdgl.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.innerApi.common.requestDto.CommonParam;
import org.jeecg.modules.qyxx.entity.CsPtQyxx;
import org.jeecg.modules.qyxx.service.ICsPtQyxxService;
import org.jeecg.modules.system.entity.SysDepart;
import org.jeecg.modules.system.service.ISysDepartService;
import org.jeecg.modules.xxgx.fwqgl.entity.CsXxFwqxx;
import org.jeecg.modules.xxgx.fwqgl.service.ICsXxFwqxxService;
import org.jeecg.modules.xxgx.kpzdgl.entity.CsXxKpzd;
import org.jeecg.modules.xxgx.kpzdgl.mapper.CsXxKpzdMapper;
import org.jeecg.modules.xxgx.kpzdgl.service.ICsXxKpzdService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Description: 开票终端
 * @Author: jeecg-boot
 * @Date:   2023-08-19
 * @Version: V1.0
 */
@Service
public class CsXxKpzdServiceImpl extends ServiceImpl<CsXxKpzdMapper, CsXxKpzd> implements ICsXxKpzdService {

    @Autowired
    private ICsXxFwqxxService fwqxxService;
    @Autowired
    private ICsPtQyxxService qyxxService;

    @Autowired
    private ISysDepartService sysDepartService;
    /**
     * 根据开票终端数据，查询完善终端信息、服务器信息、企业信息
     * @param kpzddm 开票终端代码
     * @return
     */
    public CommonParam getCommonParamByKpzd(String kpzddm){
        //获取开票终端信息
        List<CsXxKpzd> csXxKpzds = this.lambdaQuery().eq(CsXxKpzd::getZddm, kpzddm).list();
        CommonParam param=new CommonParam();
        if (csXxKpzds.size() > 0) {
            //设置开票终端
            CsXxKpzd csXxKpzd = csXxKpzds.get(0);
            param.setKpzd(csXxKpzd);
            String fwqid = csXxKpzd.getFwqid();
            if(!StringUtils.isEmpty(fwqid)) {
                CsXxFwqxx fwqxx = fwqxxService.getById(fwqid);
                param.setFwqxx(fwqxx);
            }
           //获取企业信息
            CsPtQyxx qyxx=qyxxService.getById(csXxKpzd.getCompanyId());
            if(StringUtils.isNotEmpty(qyxx.getZzjgbm())) {
                SysDepart sysDepart = sysDepartService.getDepartById(qyxx.getZzjgbm());
                qyxx.setZzjgOrgCode(sysDepart.getOrgCode());
            }

            param.setQyxx(qyxx);
        }
        return param;
    }

    /**
     * 切换默认终端
     * @param companyId   企业编码
     * @param zdlx   终端类型
     * @param zddm 终端代码
     */
    public Result changeMrzd(String companyId, String zdlx, String zddm){
        List<CsXxKpzd> csXxKpzds = this.lambdaQuery().eq(CsXxKpzd::getZdlx, zdlx).eq(CsXxKpzd::getCompanyId,companyId).eq(CsXxKpzd::getIsmrzd,"是").list();
        if(csXxKpzds!=null&&csXxKpzds.size()>0){
            for(CsXxKpzd kpzd:csXxKpzds){
                kpzd.setIsmrzd("否");
                this.updateById(kpzd);
            }
        }
        CsXxKpzd csXxKpzd=this.lambdaQuery().eq(CsXxKpzd::getZddm,zddm).one();
        if(csXxKpzd!=null) {
            csXxKpzd.setIsmrzd("是");
            this.updateById(csXxKpzd);
        }
        return Result.OK();
    }

}
